package File;

import java.io.*;

public class CopyFile {

    public static void main(String[] args) {
        getEncoding();
    }

    public static void getEncoding(){
        System.getProperties().list(System.out);
    }

    public static void copyFileHandle(){
        File file = new File("a/c.txt");
        File file1 = new File("c/d.txt");
        try {
            if (!file1.getParentFile().exists()) {
                file1.getParentFile().mkdirs();
            }
            Long start = System.currentTimeMillis();
            byte[] temp = new byte[1024];
            int len = 0;
            InputStream in = new FileInputStream(file);
            OutputStream out = new FileOutputStream(file1);
            while ((len = in.read(temp)) != -1){
                out.write(temp,0,len);
            }
            Long end = System.currentTimeMillis();
            System.out.println(end - start);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFileByByte(){
        File file = new File("a/c.txt");
        File file1 = new File("c/d.txt");

        try {
            if (!file1.getParentFile().exists()) {
                file1.getParentFile().mkdirs();
            }
            Long start = System.currentTimeMillis();
            int temp;
            InputStream in = new FileInputStream(file);
            OutputStream out = new FileOutputStream(file1);
            while ((temp = in.read()) != -1){
                out.write(temp);
            }
            Long end = System.currentTimeMillis();
            System.out.println(end - start);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void copyFileByFile(){
        File file = new File("a/c.txt");
        File file1 = new File("c/d.txt");

        try {
            if (!file1.getParentFile().exists()) {
                file1.getParentFile().mkdirs();
            }
            file1.renameTo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
